home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2001 September / PC-WELT 9-2001.ISO / software / hw / brennen / flask_src.exe / Audio / MPEG / Layer3.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-06  |  34.2 KB  |  1,366 lines

  1. /* 
  2.  *  Layer3.cpp
  3.  *
  4.  *  Code from
  5.  *            NekoAmp 1.3 decoder by Avery Lee
  6.  *
  7.  *  FlasKMPEG
  8.  *    Copyright (C) Alberto Vigata - January 2000
  9.  *
  10.  *  This file is part of FlasKMPEG, a free MPEG to MPEG/AVI converter
  11.  *    
  12.  *  FlasKMPEG is free software; you can redistribute it and/or modify
  13.  *  it under the terms of the GNU General Public License as published by
  14.  *  the Free Software Foundation; either version 2, or (at your option)
  15.  *  any later version.
  16.  *   
  17.  *  FlasKMPEG is distributed in the hope that it will be useful,
  18.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  *  GNU General Public License for more details.
  21.  *   
  22.  *  You should have received a copy of the GNU General Public License
  23.  *  along with GNU Make; see the file COPYING.  If not, write to
  24.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  25.  *
  26.  */
  27.  
  28.  
  29. #include <crtdbg.h>
  30. #include <string.h>
  31. #include <assert.h>
  32. #include <stdio.h>
  33. #include <math.h>
  34.  
  35. #include "AMPDecoder.h"
  36.  
  37. ////////////////////////////////////////////////////////////////////////////
  38.  
  39. //#define RDTSC_PROFILE
  40.  
  41. #ifdef RDTSC_PROFILE
  42.  
  43.     #include <windows.h>
  44.  
  45.     static long p_lasttime;
  46.     static long p_frames=0;
  47.     static __int64 p_total=0;
  48.     static __int64 p_header=0;
  49.     static __int64 p_scalefac=0;
  50.     static __int64 p_huffdec=0;
  51.     static __int64 p_dequan=0;
  52.     static __int64 p_stereo=0;
  53.     static __int64 p_antialias=0;
  54.     static __int64 p_hybrid=0;
  55.     static __int64 p_polyphase1=0;
  56.     static __int64 p_polyphase2=0;
  57.  
  58.     static void __inline profile_set(int) {
  59.         __asm {
  60.             rdtsc
  61.             mov        p_lasttime,eax
  62.         };
  63.     }
  64.  
  65.     static void __inline profile_add(__int64& counter) {
  66.         long diff;
  67.  
  68.         __asm {
  69.             rdtsc
  70.             sub        eax,p_lasttime
  71.             mov        diff,eax
  72.         }
  73.  
  74.         counter += diff;
  75.         p_total += diff;
  76.  
  77.         __asm {
  78.             rdtsc
  79.             mov        p_lasttime,eax
  80.  
  81.         }
  82.     }
  83. #else
  84.  
  85.     #define profile_set(x)
  86.     #define profile_add(x)
  87.  
  88. #endif
  89.  
  90. ////////////////////////////////////////////////////////////////////////////
  91.  
  92. int AMPDecoder::L3_GetBits(int bits) {
  93.     unsigned char *ptr = l3bitptr + (l3bitidx>>3);
  94.     unsigned long bitheap;
  95.  
  96.     bitheap = ((unsigned long)ptr[0] << 24)
  97.             + ((unsigned long)ptr[1] << 16)
  98.             + ((unsigned long)ptr[2] << 8)
  99.             + (unsigned long)ptr[3];
  100.  
  101.     bitheap <<= l3bitidx & 7;
  102.     bitheap >>= 32-bits;
  103.  
  104.     l3bitidx += bits;
  105.  
  106.     return (int)bitheap;
  107. }
  108.  
  109. void AMPDecoder::L3_GetSideInfo() {
  110.     unsigned char heap[32];
  111.     int gr, ch, i;
  112.     
  113.     l3bitptr = heap;
  114.     l3bitidx = 0;
  115.  
  116.     if (sideinfo_size != pSource->read(heap, sideinfo_size))
  117.         throw (int)ERR_EOF;
  118.  
  119.     // process side info
  120.  
  121.     if (is_mpeg2) {
  122.         sideinfo.main_data_begin        = L3_GetBits(8);
  123.         if (mode == MODE_MONO)
  124.             sideinfo.private_bits        = L3_GetBits(1);
  125.         else
  126.             sideinfo.private_bits        = L3_GetBits(2);
  127.     } else {
  128.         sideinfo.main_data_begin        = L3_GetBits(9);
  129.         if (mode == MODE_MONO)
  130.             sideinfo.private_bits        = L3_GetBits(5);
  131.         else
  132.             sideinfo.private_bits        = L3_GetBits(3);
  133.  
  134.         // read in scale factor selectors
  135.  
  136.         for(ch=0; ch<channels; ch++)
  137.             for(i=0; i<4; i++)
  138.                 sideinfo.ch[ch].scfsi[i] = L3_GetBits(1);
  139.  
  140.     }
  141.  
  142.     // read in data per region/channel
  143.  
  144.     for(gr=0; gr<(is_mpeg2?1:2); gr++) {
  145.         for(ch=0; ch<channels; ch++) {
  146.             sideinfo.ch[ch].gr[gr].part2_3_length = L3_GetBits(12);
  147.             sideinfo.ch[ch].gr[gr].big_values = L3_GetBits(9);
  148.             sideinfo.ch[ch].gr[gr].global_gain = L3_GetBits(8);
  149.  
  150.             if (is_mpeg2)
  151.                 sideinfo.ch[ch].gr[gr].scalefac_compress = L3_GetBits(9);
  152.             else
  153.                 sideinfo.ch[ch].gr[gr].scalefac_compress = L3_GetBits(4);
  154.  
  155.             sideinfo.ch[ch].gr[gr].window_switching_flag = L3_GetBits(1);
  156.             if (sideinfo.ch[ch].gr[gr].window_switching_flag) {
  157.                 sideinfo.ch[ch].gr[gr].block_type = L3_GetBits(2);
  158.                 sideinfo.ch[ch].gr[gr].mixed_block_flag = L3_GetBits(1);
  159.                 for (i=0; i<2; i++)
  160.                     sideinfo.ch[ch].gr[gr].table_select[i] = L3_GetBits(5);
  161.                 for (i=0; i<3; i++)
  162.                     sideinfo.ch[ch].gr[gr].subblock_gain[i] = L3_GetBits(3);
  163.                
  164.                 /* Set region_count parameters since they are implicit in this case. */
  165.             
  166.                 if (sideinfo.ch[ch].gr[gr].block_type == 0)
  167.                     throw (int)ERR_INTERNAL;    // bad?
  168.                 else if (sideinfo.ch[ch].gr[gr].block_type == 2
  169.                      && sideinfo.ch[ch].gr[gr].mixed_block_flag == 0)
  170.                     sideinfo.ch[ch].gr[gr].region0_count = 8; /* MI 9; */
  171.                 else
  172.                     sideinfo.ch[ch].gr[gr].region0_count = 7; /* MI 8; */
  173.  
  174.                 sideinfo.ch[ch].gr[gr].region1_count = 20 -
  175.                         sideinfo.ch[ch].gr[gr].region0_count;
  176.             } else {
  177.                 for (i=0; i<3; i++)
  178.                     sideinfo.ch[ch].gr[gr].table_select[i] = L3_GetBits(5);
  179.                 sideinfo.ch[ch].gr[gr].region0_count = L3_GetBits(4);
  180.                 sideinfo.ch[ch].gr[gr].region1_count = L3_GetBits(3);
  181.                 sideinfo.ch[ch].gr[gr].block_type = 0;
  182.             }
  183.  
  184.             if (!is_mpeg2)
  185.                 sideinfo.ch[ch].gr[gr].preflag = L3_GetBits(1);
  186.  
  187.             sideinfo.ch[ch].gr[gr].scalefac_scale = L3_GetBits(1);
  188.             sideinfo.ch[ch].gr[gr].count1table_select = L3_GetBits(1);
  189.         }
  190.     }
  191. }
  192.  
  193. void AMPDecoder::L3_PrereadFrame() {
  194.     fillbits(frame_size);
  195. }
  196.  
  197. bool AMPDecoder::L3_DecodeFrame() {
  198.     int gr, ch, sb, ss;
  199.  
  200.     profile_set(0);
  201.  
  202.     // bring in new bits
  203.  
  204.     fillbits(frame_size);
  205.  
  206.     // rewind read point back to beginning of data for this frame
  207.  
  208. //_RPT2(0,"Layer III: added %ld bytes, rewind to %ld\n", frame_size, sideinfo.main_data_begin + frame_size);
  209.  
  210.     rewind(sideinfo.main_data_begin + frame_size);
  211.  
  212.     profile_add(p_header);
  213.  
  214.     // process granules.
  215.     //
  216.     // 2 for MPEG-1, 1 for MPEG-2
  217.  
  218.     for (gr=0;gr<(is_mpeg2 ? 1 : 2);gr++) {
  219.         float lr[2][SBLIMIT][SSLIMIT],ro[SBLIMIT][SSLIMIT];
  220.         III_scalefac_t scalefac;
  221.         int sample_end[2];
  222.         int new_end;
  223.  
  224.         // process each channel in the granule.
  225.         
  226.         for (ch=0; ch<channels; ch++) {
  227.             long int is[SBLIMIT*SSLIMIT];   /* Quantized samples. */
  228.             int part2_start;
  229.             part2_start = tellbits();
  230.             int limit;
  231.  
  232.             // decode scale factors
  233.  
  234.             if (is_mpeg2)
  235.                 L3_GetScaleFactors2(&scalefac, ch);
  236.             else
  237.                 L3_GetScaleFactors1(&scalefac, gr, ch);
  238.  
  239.             profile_add(p_scalefac);
  240.  
  241.             // decode samples
  242.  
  243.             limit = L3_HuffmanDecode(is, ch, gr, part2_start);
  244.  
  245.             profile_add(p_huffdec);
  246.  
  247.             // dequantize samples
  248.  
  249.             sample_end[ch] = L3_DequantizeSample(is, (float *)lr[ch], &scalefac[ch],
  250.                 &sideinfo.ch[ch].gr[gr], ch, limit);
  251.  
  252.             profile_add(p_dequan);
  253.         }
  254.  
  255.         // process joint stereo in lr
  256.  
  257.         new_end = L3_Stereo((float (*)[576])lr, &scalefac, &sideinfo.ch[0].gr[gr], sample_end[0], sample_end[1]);
  258.  
  259.         if (new_end > 0)
  260.             sample_end[0] = sample_end[1] = new_end;
  261.  
  262.         profile_add(p_stereo);
  263.  
  264.         float polyPhaseIn[2][SSLIMIT][SBLIMIT];
  265.             
  266.         for (ch=0; ch<channels; ch++) {
  267.             
  268.             // reorder samples; use 'hybridOut' as our out here, to reuse memory
  269.  
  270.             L3_Reorder((float *)lr[ch],(float *)ro,&sideinfo.ch[ch].gr[gr]);
  271.  
  272.             // antialiasing
  273.  
  274.             L3_Antialias(lr[ch], /* Antialias butterflies. */
  275.                 &sideinfo.ch[ch].gr[gr]);
  276.  
  277.             profile_add(p_antialias);
  278.  
  279.             // Hybrid synthesis.
  280.  
  281.             L3_Hybrid(lr[ch], polyPhaseIn[ch], ch, &sideinfo.ch[ch].gr[gr], sample_end[ch]);
  282.  
  283.             profile_add(p_hybrid);
  284.         }
  285.  
  286.         if (mode == MODE_MONO)
  287.             for(ss=0; ss<18; ss++)
  288.                 polyphase(polyPhaseIn[0][ss], NULL, psDest + 32*ss + 576*gr, !!(ss&1));
  289.         else
  290.             for(ss=0; ss<18; ss++)
  291.                 polyphase(polyPhaseIn[0][ss], polyPhaseIn[1][ss], psDest + 64*ss + 1152*gr, !!(ss&1));
  292.  
  293.         profile_add(p_polyphase2);
  294.     }
  295.  
  296. #ifdef RDTSC_PROFILE
  297.  
  298.     if (!(++p_frames & 127)) {
  299.         static char buf[256];
  300.  
  301.         sprintf(buf, "%d frames: total %I64d, header %d%%, scalefac %d%%, huffdec %d%%, dequan %d%%, stereo %d%%, antialias %d%%, hybrid %d%%, poly %d%%/%d%%\n"
  302.                 ,p_frames
  303.                 ,p_total
  304.                 ,(long)((p_header*100)/p_total)
  305.                 ,(long)((p_scalefac*100)/p_total)
  306.                 ,(long)((p_huffdec*100)/p_total)
  307.                 ,(long)((p_dequan*100)/p_total)
  308.                 ,(long)((p_stereo*100)/p_total)
  309.                 ,(long)((p_antialias*100)/p_total)
  310.                 ,(long)((p_hybrid*100)/p_total)
  311.                 ,(long)((p_polyphase1*100)/p_total)
  312.                 ,(long)((p_polyphase2*100)/p_total)
  313.                 );
  314.         OutputDebugString(buf);
  315.     }
  316. #endif
  317.  
  318.     // set sample count
  319.  
  320.     lSampleCount = 1152;
  321.     if (is_mpeg2)
  322.         lSampleCount = 576;
  323.  
  324.     if (channels>1)
  325.         lSampleCount <<=1;
  326.  
  327. //    _RPT2(0,"Layer III: terminated with %ld bits left (%ld bytes)\n", tellbits(), tellbits()/8);
  328.              
  329.     return true;
  330. }
  331.  
  332. struct {
  333.    int l[5];
  334.    int s[3];} sfbtable = {{0, 6, 11, 16, 21},
  335.                           {0, 6, 12}};
  336.                          
  337. const int slen1[2][16] = {
  338.     {0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4},
  339.     {0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3}
  340. };
  341.  
  342. void AMPDecoder::L3_GetScaleFactors1(III_scalefac_t *scalefac,
  343.                             int gr,
  344.                             int ch) {
  345.  
  346.     int sfb, i, window;
  347.     struct gr_info_s *gr_info = &sideinfo.ch[ch].gr[gr];
  348.  
  349.     if (gr_info->window_switching_flag && (gr_info->block_type == 2)) { 
  350.         if (gr_info->mixed_block_flag) { /* MIXED */ /* NEW - ag 11/25 */
  351.  
  352.             for (sfb = 0; sfb < 8; sfb++) {
  353.                 (*scalefac)[ch].l[sfb] = getbits(slen1[0][gr_info->scalefac_compress]);
  354.             }
  355.  
  356.             for (sfb = 3; sfb < 6; sfb++)
  357.                 for (window=0; window<3; window++) {
  358.                     (*scalefac)[ch].s[window][sfb] = getbits(slen1[0][gr_info->scalefac_compress]);
  359.                 }
  360.  
  361.             for (sfb = 6; sfb < 12; sfb++)
  362.                 for (window=0; window<3; window++) {
  363.                     (*scalefac)[ch].s[window][sfb] = getbits(slen1[1][gr_info->scalefac_compress]);
  364.                 }
  365.                     
  366.             for (window=0; window<3; window++)
  367.                 (*scalefac)[ch].s[window][12] = 0;
  368.  
  369.         } else {  /* SHORT*/
  370.  
  371.             for (i=0; i<2; i++) 
  372.                 for (sfb = sfbtable.s[i]; sfb < sfbtable.s[i+1]; sfb++)
  373.                     for (window=0; window<3; window++) {
  374.                         (*scalefac)[ch].s[window][sfb] = getbits(slen1[i][gr_info->scalefac_compress]);
  375.                     }
  376.  
  377.             for (window=0; window<3; window++)
  378.                 (*scalefac)[ch].s[window][12] = 0;
  379.         }
  380.     } else {   /* LONG types 0,1,3 */
  381.  
  382.         for (i=0; i<2; i++) {
  383.             if ((sideinfo.ch[ch].scfsi[i] == 0) || (gr == 0))
  384.                 for (sfb = sfbtable.l[i]; sfb < sfbtable.l[i+1]; sfb++) {
  385.                     (*scalefac)[ch].l[sfb] = getbits(slen1[0][gr_info->scalefac_compress]);
  386.                 }
  387.         }
  388.         for (i=2; i<4; i++) {
  389.             if ((sideinfo.ch[ch].scfsi[i] == 0) || (gr == 0))
  390.                 for (sfb = sfbtable.l[i]; sfb < sfbtable.l[i+1]; sfb++) {
  391.                     (*scalefac)[ch].l[sfb] = getbits(slen1[1][gr_info->scalefac_compress]);
  392.                 }
  393.         }
  394.         (*scalefac)[ch].l[21] = 0; 
  395.         (*scalefac)[ch].l[22] = 0; 
  396.     }
  397.  
  398. }
  399.  
  400. void AMPDecoder::L3_GetScaleFactors2(III_scalefac_t *scalefac, int ch) {
  401.     static const int sfbblockindex[6][3][4]={
  402.         {{ 6, 5, 5, 5},{ 9, 9, 9, 9},{ 6, 9, 9, 9}},
  403.         {{ 6, 5, 7, 3},{ 9, 9,12, 6},{ 6, 9,12, 6}},
  404.         {{11,10, 0, 0},{18,18, 0, 0},{15,18, 0, 0}},
  405.         {{ 7, 7, 7, 0},{12,12,12, 0},{ 6,15,12, 0}},
  406.         {{ 6, 6, 6, 3},{12, 9, 9, 6},{ 6,12, 9, 6}},
  407.         {{ 8, 8, 5, 0},{15,12, 9, 0},{ 6,18, 9, 0}}
  408.     };
  409.  
  410.     int sb[54];
  411.     struct gr_info_s *gi=&(sideinfo.ch[ch].gr[0]);
  412.     struct III_scalefac1_t *sf=(&(*scalefac)[ch]);
  413.  
  414.     {
  415.         int blocktypenumber,sc;
  416.         int blocknumber;
  417.         
  418.         if(gi->block_type==2)blocktypenumber=1+gi->mixed_block_flag;
  419.         else blocktypenumber=0;
  420.         
  421.         sc=gi->scalefac_compress;
  422.         if(!((mode_ext==1 || mode_ext==3) && (ch==1)))
  423.         {
  424.             if(sc<400) {
  425.                 slen[0]=(sc>>4)/5;
  426.                 slen[1]=(sc>>4)%5;
  427.                 slen[2]=(sc%16)>>2;
  428.                 slen[3]=(sc%4);
  429.                 gi->preflag=0;
  430.                 blocknumber=0;
  431.             } else if(sc<500) {
  432.                 sc-=400;
  433.                 slen[0]=(sc>>2)/5;
  434.                 slen[1]=(sc>>2)%5;
  435.                 slen[2]=sc%4;
  436.                 slen[3]=0;
  437.                 gi->preflag=0;
  438.                 blocknumber=1;
  439.             } else {
  440.                 sc-=500;
  441.                 slen[0]=sc/3;
  442.                 slen[1]=sc%3;
  443.                 slen[2]=0;
  444.                 slen[3]=0;
  445.                 gi->preflag=1;
  446.                 blocknumber=2;
  447.             }
  448.         } else {
  449.             sc>>=1;
  450.             if(sc<180) {
  451.                 slen[0]=sc/36;
  452.                 slen[1]=(sc%36)/6;
  453.                 slen[2]=(sc%36)%6;
  454.                 slen[3]=0;
  455.                 gi->preflag=0;
  456.                 blocknumber=3;
  457.             } else if(sc<244) {
  458.                 sc-=180;
  459.                 slen[0]=(sc%64)>>4;
  460.                 slen[1]=(sc%16)>>2;
  461.                 slen[2]=sc%4;
  462.                 slen[3]=0;
  463.                 gi->preflag=0;
  464.                 blocknumber=4;
  465.             } else {
  466.                 sc-=244;
  467.                 slen[0]=sc/3;
  468.                 slen[1]=sc%3;
  469.                 slen[2]=
  470.                     slen[3]=0;
  471.                 gi->preflag=0;
  472.                 blocknumber=5;
  473.             }
  474.         }
  475.         
  476.         {
  477.             int i,j,k;
  478.             const int *si;
  479.             
  480.             scale_block_indexes=sfbblockindex[blocknumber][blocktypenumber];
  481.             for(i=0;i<45;i++)sb[i]=0;
  482.             
  483.             for(k=i=0;i<4;i++)
  484.                 for(j=0;j<scale_block_indexes[i];j++,k++)
  485.                     if(slen[i]==0)sb[k]=0;
  486.                     else sb[k]=getbits(slen[i]);
  487.         }
  488.     }
  489.     
  490.     
  491.     {
  492.         int sfb,window;
  493.         int k=0;
  494.         
  495.         if(gi->window_switching_flag && (gi->block_type==2))
  496.         {
  497.             if(gi->mixed_block_flag)
  498.             {
  499.                 for(sfb=0;sfb<8;sfb++)sf->l[sfb]=sb[k++];
  500.                 sfb=3;
  501.             }
  502.             else sfb=0;
  503.             
  504.             for(;sfb<12;sfb++)
  505.                 for(window=0;window<3;window++)
  506.                     sf->s[window][sfb]=sb[k++];
  507.                 
  508.                 sf->s[0][12]=sf->s[1][12]=sf->s[2][12]=0;
  509.         }
  510.         else
  511.         {
  512.             for(sfb=0;sfb<21;sfb++)
  513.                 sf->l[sfb]=sb[k++];
  514.             sf->l[21]=sf->l[22]=0;
  515.         }
  516.     }
  517. }
  518.  
  519. static struct  {
  520.     int l[23];
  521.     int s[14];} sfBandIndex[2][3] =   
  522.     {
  523.         {
  524.             {{0,4,8,12,16,20,24,30,36,44,52,62,74,90,110,134,162,196,238,288,342,418,576},
  525.              {0,4,8,12,16,22,30,40,52,66,84,106,136,192}},
  526.             {{0,4,8,12,16,20,24,30,36,42,50,60,72,88,106,128,156,190,230,276,330,384,576},
  527.              {0,4,8,12,16,22,28,38,50,64,80,100,126,192}},
  528.             {{0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576},
  529.              {0,4,8,12,16,22,30,42,58,78,104,138,180,192}},
  530.         },
  531.         {
  532.             {{0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
  533.              {0,4,8,12,18,24,32,42,56,74,100,132,174,192}},
  534.             {{0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,330,394,464,540,576},
  535.              {0,4,8,12,18,26,36,48,62,80,104,136,180,192}},
  536.             {{0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
  537.              {0,4,8,12,18,26,36,48,62,80,104,134,174,192}},
  538.         }
  539.    };
  540.  
  541. int AMPDecoder::L3_HuffmanDecode(long int is[SBLIMIT*SSLIMIT],
  542.                         int ch,
  543.                         int gr,
  544.                         int part2_start) {
  545.  
  546.     int i, x, y;
  547.     int v, w;
  548.     int h;
  549.     int region1Start;
  550.     int region2Start;
  551.     int bt = sideinfo.ch[ch].gr[gr].window_switching_flag && (sideinfo.ch[ch].gr[gr].block_type == 2);
  552.         
  553.     /* Find region boundary for short block case. */
  554.     
  555.     if ( (sideinfo.ch[ch].gr[gr].window_switching_flag) && 
  556.         (sideinfo.ch[ch].gr[gr].block_type == 2) ) { 
  557.         
  558.         /* Region2. */
  559.         
  560.         region1Start = 36;  /* sfb[9/3]*3=36 */
  561.         region2Start = 576; /* No Region2 for short block case. */
  562.     }
  563.     
  564.     
  565.     else {          /* Find region boundary for long block case. */
  566.         
  567.         region1Start = sfBandIndex[is_mpeg2][sr_index].l[sideinfo.ch[ch].gr[gr].region0_count + 1]; /* MI */
  568.         region2Start = sfBandIndex[is_mpeg2][sr_index].l[sideinfo.ch[ch].gr[gr].region0_count + sideinfo.ch[ch].gr[gr].region1_count + 2]; /* MI */
  569.     }
  570.     
  571. //_RPT1(0,"%d bits left (bigvalues)\n", tellbits());
  572.     
  573.     /* Read bigvalues area. */
  574.  
  575.     if (region1Start > sideinfo.ch[ch].gr[gr].big_values*2)
  576.         region1Start = sideinfo.ch[ch].gr[gr].big_values*2;
  577.  
  578.     if (region2Start > sideinfo.ch[ch].gr[gr].big_values*2)
  579.         region2Start = sideinfo.ch[ch].gr[gr].big_values*2;
  580.  
  581.     h = sideinfo.ch[ch].gr[gr].table_select[0];
  582.  
  583.     if (h)
  584.         L3_GetHuffmanBig(h, &is[0], region1Start);
  585.     else
  586.         memset(is, 0, sizeof(is[0])*region1Start);
  587.  
  588.     i = region1Start;
  589.  
  590.     if (i < region2Start) {
  591.         h = sideinfo.ch[ch].gr[gr].table_select[1];
  592.  
  593.         if (h)
  594.             L3_GetHuffmanBig(h, &is[i], region2Start - i);
  595.         else
  596.             memset(is+i, 0, sizeof(is[0])*(region2Start-i));
  597.     }
  598.  
  599.     i = region2Start;
  600.  
  601.     if (i < sideinfo.ch[ch].gr[gr].big_values*2) {
  602.         h = sideinfo.ch[ch].gr[gr].table_select[2];
  603.  
  604.         if (h)
  605.             L3_GetHuffmanBig(h, &is[i], sideinfo.ch[ch].gr[gr].big_values*2 - i);
  606.         else
  607.             memset(is+i, 0, sizeof(is[0])*(sideinfo.ch[ch].gr[gr].big_values*2-i));
  608.     }
  609.  
  610.     i = sideinfo.ch[ch].gr[gr].big_values*2;
  611.  
  612.     /* Read count1 area. */
  613.  
  614.     if (sideinfo.ch[ch].gr[gr].count1table_select) {
  615.         int count;
  616.  
  617.         // fixed 4-bit table
  618.  
  619.         i = L3_GetHuffmanCount1_33(&is[i], i, part2_start - sideinfo.ch[ch].gr[gr].part2_3_length);
  620.  
  621.     } else {
  622.         // vbr huffman table
  623.  
  624.         i = L3_GetHuffmanCount1_32(&is[i], i, part2_start - sideinfo.ch[ch].gr[gr].part2_3_length);
  625.     }
  626.  
  627. //    _RPT2(0,"%d big values, %d small values\n", sideinfo.ch[ch].gr[gr].big_values*2, i - sideinfo.ch[ch].gr[gr].big_values*2);
  628.  
  629.  
  630.     if (tellbits() < part2_start - sideinfo.ch[ch].gr[gr].part2_3_length)
  631.         i -= 4;
  632.  
  633. //_RPT2(0,"%d bits left, attempting rewind to %d\n", tellbits(), part2_start - sideinfo.ch[ch].gr[gr].part2_3_length);
  634.  
  635.     rewindbits(part2_start - sideinfo.ch[ch].gr[gr].part2_3_length);
  636.     
  637.     /* Zero out rest. */
  638.  
  639.     return i;
  640. }
  641.  
  642. static const int pretab[22] = {0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,2,2,3,3,3,2,0};
  643.  
  644. // pow43[15 + i] = abs(i) ^ (4/3) * sign(i)
  645.  
  646. static const double pow43[32]={
  647.     -36.99318111495705,
  648.     -33.74199169845322,
  649.     -30.56735094036985,
  650.     -27.47314182127996,
  651.     -24.46378099626247,
  652.     -21.54434690031884,
  653.     -18.72075440746714,
  654.     -16.00000000000000,
  655.     -13.39051827940672,
  656.     -10.90272355699284,
  657.     -8.549879733383484,
  658.     -6.349604207872798,
  659.     -4.326748710922225,
  660.     -2.519842099789746,
  661.     -1.000000000000000,
  662.      0,
  663.      1.000000000000000,
  664.      2.519842099789746,
  665.      4.326748710922225,
  666.      6.349604207872798,
  667.      8.549879733383484,
  668.      10.90272355699284,
  669.      13.39051827940672,
  670.      16.00000000000000,
  671.      18.72075440746714,
  672.      21.54434690031884,
  673.      24.46378099626247,
  674.      27.47314182127996,
  675.      30.56735094036985,
  676.      33.74199169845322,
  677.      36.99318111495705,
  678.      40.31747359663594,
  679. };
  680.  
  681. int AMPDecoder::L3_DequantizeSample(long int is[SBLIMIT*SSLIMIT],
  682.                             float xr[SBLIMIT*SSLIMIT],
  683.                             const III_scalefac1_t *scaleptr,
  684.                             struct gr_info_s *gr_info,
  685.                             int ch, int limit) {
  686.  
  687.     int ind,cb=0,sfreq=sr_index;
  688.     int stereo = channels;
  689.     int next_cb_boundary, next_cb2_boundary, cb_width, sign;
  690.     int last_nonzero = -1;
  691.  
  692.  
  693.     // We optimize all the pow() out of this routine.
  694.     //
  695.     //    gr_info->global_gain:        0-255 (8 bits)
  696.     //    gr_info->scalefac_scale:    0 or 1
  697.     //    gr_info->preflag:            0 or 1
  698.     //    gr_info->subblock_gain:        0-7 (3 bits)
  699.     //    scalefac[ch].s[]:            0-15 (4 bits max)
  700.     //    scalefac[ch].l[]:            0-15 (4 bits max)
  701.     //    pretab[]:                    0-3
  702.     //
  703.     //    Let f(x) = 2^(0.25*x), where f(a)f(b) = f(a+b).
  704.     //
  705.     //    xr0 = f(gr_info->global_gain - 210);
  706.     //        range: f(-210) to f(45).
  707.     //
  708.     // short blocks:
  709.     //
  710.     //        xr = xr0 * f(-8.0 * gr_info->subblock_gain[(ind - cb_begin)/cb_width]
  711.     //                -2.0 * (1.0+gr_info->scalefac_scale)
  712.     //            * (*scalefac)[ch].s[(ind - cb_begin)/cb_width][cb]);
  713.     //
  714.     //        range: f(-116) to f(0).
  715.     //
  716.     // LONG block types 0,1,3 & 1st 2 subbands of switched blocks:
  717.     //
  718.     //        xr = xr0 * f(-2.0 * (1.0+gr_info->scalefac_scale)
  719.     //            * ((*scalefac)[ch].l[cb]
  720.     //            + gr_info->preflag * pretab[cb]));
  721.     //
  722.     //        range: f(-72) to f(0).
  723.     //
  724.     //    CONCLUSION:
  725.     //
  726.     //        Necessary range is f(-326) to f(45).
  727.     //
  728.     //    We negate the sign, so it's really f(-45) to f(326).  Scaling
  729.     //    to 16-bit PCM is also done here.
  730.  
  731.     static float pow2tbl[326+45+1];
  732.     static bool init_flag = true;
  733.     double xr_val;
  734.     int        scalefac_shift = 1+gr_info->scalefac_scale;
  735.     int        preflag_mask = gr_info->preflag ? ~0 : 0;
  736.     int        cb_low, cb_high;
  737.  
  738.     if (init_flag) {
  739.         init_flag = false;
  740.  
  741.         for(int i=-45; i<=326; i++)
  742.             pow2tbl[i+45] = pow(2.0, -0.25 * i) * 32768.0;
  743.     }
  744.  
  745.  
  746.  
  747.     // global_gain ranges from 0-255 (8 bits)
  748.  
  749.     float *xr0_ptr = pow2tbl + 45 + 210 - (int)gr_info->global_gain;
  750.     
  751.     /* choose correct scalefactor band per block type, initalize boundary */
  752.     
  753.     bool    fShortBlock        = gr_info->window_switching_flag && (gr_info->block_type == 2);
  754.     int        iScaleThreshold = gr_info->mixed_block_flag ? 2*SSLIMIT : 0;
  755.  
  756.     next_cb_boundary = 0;
  757.     cb = -1;
  758.         
  759.     /* apply formula per block type */
  760.  
  761.     for (ind=0 ; ind < limit ; ind++) {
  762.  
  763.         if ( ind == next_cb_boundary)  { /* Adjust critical band boundary */
  764.  
  765.             if (fShortBlock) {
  766.  
  767.                 if (gr_info->mixed_block_flag) {
  768.                     if (ind == sfBandIndex[is_mpeg2][sfreq].l[8])  {
  769.                         next_cb_boundary=sfBandIndex[is_mpeg2][sfreq].s[4]*3; 
  770.                         cb = 3;
  771.                         cb_width = sfBandIndex[is_mpeg2][sfreq].s[cb+1] - sfBandIndex[is_mpeg2][sfreq].s[cb];
  772.                         
  773.                     } else if (ind < sfBandIndex[is_mpeg2][sfreq].l[8]) {
  774.  
  775.                         next_cb_boundary = sfBandIndex[is_mpeg2][sfreq].l[(++cb)+1];
  776.  
  777.                     } else {
  778.                         next_cb_boundary = sfBandIndex[is_mpeg2][sfreq].s[(++cb)+1]*3;
  779.                         cb_width = sfBandIndex[is_mpeg2][sfreq].s[cb+1] - sfBandIndex[is_mpeg2][sfreq].s[cb];
  780.                     }   
  781.                 } else {
  782.                     next_cb_boundary = sfBandIndex[is_mpeg2][sfreq].s[(++cb)+1]*3;
  783.                     cb_width = sfBandIndex[is_mpeg2][sfreq].s[cb+1] - sfBandIndex[is_mpeg2][sfreq].s[cb];
  784.                 } 
  785.  
  786.             } else /* long blocks */
  787.                 next_cb_boundary = sfBandIndex[is_mpeg2][sfreq].l[(++cb)+1];
  788.  
  789.             // block type 2?
  790.  
  791.             if (fShortBlock && ind >= iScaleThreshold) {
  792.                 xr_val = xr0_ptr[8*gr_info->subblock_gain[0] + (scaleptr->s[0][cb] << scalefac_shift)];
  793.                 cb_high = 0;
  794.                 next_cb2_boundary = ind + cb_width;
  795.             } else {
  796.  
  797.                 // long block, or first two subbands of switched blocks
  798.  
  799.                 xr_val = xr0_ptr[(scaleptr->l[cb] + (pretab[cb] & preflag_mask)) << scalefac_shift];
  800.                 next_cb2_boundary = -1;
  801.             }
  802.         }
  803.  
  804.         // Do we need to switch the scaling factor for short blocks?
  805.  
  806.         if (ind == next_cb2_boundary) {
  807.             ++cb_high;
  808.             xr_val = xr0_ptr[8*gr_info->subblock_gain[cb_high] + (scaleptr->s[cb_high][cb] << scalefac_shift)];
  809.             next_cb2_boundary += cb_width;
  810.         }
  811.         
  812.         /* Scale quantized value. */
  813.  
  814.         if (is[ind]) {
  815.             int ind2 = 15 + is[ind];
  816.             double y;
  817.  
  818.             if (ind2 & 0xffffffe0) {
  819.  
  820.                 if (ind2<0)
  821.                     y = -pow((double)-is[ind], (double)(4.0/3.0));
  822.                 else
  823.                     y = pow((double)is[ind], (double)(4.0/3.0));
  824.  
  825.             } else {
  826.  
  827.                 y = pow43[ind2];
  828.  
  829.             }
  830.  
  831.             xr[ind] = xr_val * y;
  832.  
  833.             last_nonzero = ind;
  834.         } else {
  835.             xr[ind] = 0.0;
  836.         }
  837.     }
  838.  
  839.     while(ind<SBLIMIT*SSLIMIT)
  840.         xr[ind++] = 0.0;
  841.  
  842.     return last_nonzero+1;
  843. }
  844.  
  845. // qbasic is the ultimate table generator
  846. //
  847. //    Intensity stereo for MPEG-1:
  848. //
  849. //    is_ratio[i][0] = 1/(1+y)
  850. //    is_ratio[i][1] = y/(1+y)
  851. //
  852. //    where y=tan(i * (3.1415926535897932 / 12));
  853. //
  854. //    Alternatively (thank you FreeAmp, blah Fraunhofer):
  855. //
  856. //    is_ratio[i][0] = s/(s+c)
  857. //    is_ratio[i][1] = c/(s+c)
  858. //
  859. //    where    s = sin(i * pi/12)
  860. //            c = cos(i * pi/12)
  861.  
  862. static const double is_ratio1[16][2]={
  863.  
  864. #define T(y) { (y)/(1.0+(y)), 1.0/(1.0+(y)) }
  865.  
  866.     T(0),
  867.     T( .2679491924311227),
  868.     T( .5773502691896257),
  869.     T( .9999999999999999),
  870.     T( 1.732050807568877),
  871.     T( 3.732050807568877),
  872.     T( 1.632455227761907e+16),
  873.     T(-3.732050807568879),
  874.     T(-1.732050807568878),
  875.  
  876. //    T(-1),    stupid asymptotes
  877.     { -1000000, 1000000 },
  878.  
  879.     T(-.5773502691896260),
  880.     T(-.2679491924311228),
  881.     T(-1.22514845490862e-16),
  882.     T( .2679491924311226),
  883.     T( .5773502691896256),
  884.     T( .9999999999999997),
  885.  
  886. #undef T
  887. };
  888.  
  889. // Intensity stereo for MPEG-2:
  890. //
  891. //
  892.  
  893. int AMPDecoder::L3_Stereo(
  894.                 float lr[2][SBLIMIT*SSLIMIT],
  895.                 const III_scalefac_t *scalefac,
  896.                 struct gr_info_s *gr_info,
  897.                 int nz0, int nz1) {
  898.  
  899.     int sfreq = sr_index;
  900.     int ms_stereo = (mode == MODE_JOINTSTEREO) && (mode_ext & 0x2); 
  901.     int i_stereo = (mode == MODE_JOINTSTEREO) && (mode_ext & 0x1);
  902.     int js_bound;  /* frequency line that marks the beggining of the zero part */  
  903.     int sfb,next_sfb_boundary;
  904.     int i,j,k,sb,ss,ch,is_pos[576]; 
  905.  
  906.     static float is_ratio2[2][2][64][2];
  907.     static bool is_ratio2_inited = false;
  908.  
  909.     if (!is_ratio2_inited) {
  910.         int k, n;
  911.         double t;
  912.         int intensity_scale, ms_mode, sf, sflen;
  913.         float ms_factor[2];
  914.         
  915.         
  916.         ms_factor[0] = 1.0;
  917.         ms_factor[1] = (float) sqrt(2.0);
  918.         
  919.         /* intensity stereo MPEG2 */
  920.         /* lr2[intensity_scale][ms_mode][sflen_offset+sf][left/right] */
  921.         
  922.         for (intensity_scale = 0; intensity_scale < 2; intensity_scale++)
  923.         {
  924.             t = pow(2.0, -0.25 * (1 + intensity_scale));
  925.             for (ms_mode = 0; ms_mode < 2; ms_mode++)
  926.             {
  927.                 
  928.                 n = 1;
  929.                 k = 0;
  930.                 for (sflen = 0; sflen < 6; sflen++)
  931.                 {
  932.                     for (sf = 0; sf < (n - 1); sf++, k++)
  933.                     {
  934.                         if (sf == 0)
  935.                         {
  936.                             is_ratio2[intensity_scale][ms_mode][k][0] = ms_factor[ms_mode] * 1.0f;
  937.                             is_ratio2[intensity_scale][ms_mode][k][1] = ms_factor[ms_mode] * 1.0f;
  938.                         }
  939.                         else if ((sf & 1))
  940.                         {
  941.                             is_ratio2[intensity_scale][ms_mode][k][0] =
  942.                                 (float) (ms_factor[ms_mode] * pow(t, (sf + 1) / 2));
  943.                             is_ratio2[intensity_scale][ms_mode][k][1] = ms_factor[ms_mode] * 1.0f;
  944.                         }
  945.                         else
  946.                         {
  947.                             is_ratio2[intensity_scale][ms_mode][k][0] = ms_factor[ms_mode] * 1.0f;
  948.                             is_ratio2[intensity_scale][ms_mode][k][1] =
  949.                                 (float) (ms_factor[ms_mode] * pow(t, sf / 2));
  950.                         }
  951.                     }
  952.                     
  953.                     /* illegal is_pos used to do ms processing */
  954.                     if (ms_mode == 0)
  955.                     {            /* ms_mode = 0 */
  956.                         is_ratio2[intensity_scale][ms_mode][k][0] = 1.0f;
  957.                         is_ratio2[intensity_scale][ms_mode][k][1] = 0.0f;
  958.                     }
  959.                     else
  960.                     {
  961.                         /* ms_mode = 1, in is bands is routine does ms processing */
  962.                         is_ratio2[intensity_scale][ms_mode][k][0] = 1.0f;
  963.                         is_ratio2[intensity_scale][ms_mode][k][1] = 1.0f;
  964.                     }
  965.                     k++;
  966.                     n = n + n;
  967.                 }
  968.             }
  969.         }
  970.  
  971.         is_ratio2_inited = true;
  972.     }
  973.  
  974.     if (channels<2) {
  975.         return -1;
  976.     }
  977.     
  978.     if (i_stereo) {
  979.  
  980.         /* intialization */
  981.         for ( i=0; i<576; i++ )
  982.             is_pos[i] = 7;
  983.  
  984.         if (gr_info->window_switching_flag && (gr_info->block_type == 2)) {
  985.             if( gr_info->mixed_block_flag ) {
  986.                 int max_sfb = 0;
  987.                 
  988.                 for ( j=0; j<3; j++ ) {
  989.                     int sfbcnt;
  990.                     
  991.                     sfbcnt = 2;
  992.                     
  993.                     for( sfb=12; sfb >=3; sfb-- ) {
  994.                         int lines;
  995.                         
  996.                         lines = sfBandIndex[is_mpeg2][sfreq].s[sfb+1]-sfBandIndex[is_mpeg2][sfreq].s[sfb];
  997.                         i = 3*sfBandIndex[is_mpeg2][sfreq].s[sfb] + (j+1) * lines - 1;
  998.                         
  999.                         while ( lines > 0 ) {
  1000.                             if ( lr[1][i] != 0.0 ) {
  1001.                                 sfbcnt = sfb;
  1002.                                 goto found_nonzero;
  1003.                             }
  1004.                             lines--;
  1005.                             i--;
  1006.                         }
  1007.                     }
  1008. found_nonzero:
  1009.                     sfb = sfbcnt + 1;
  1010.                     
  1011.                     if ( sfb > max_sfb )
  1012.                         max_sfb = sfb;
  1013.                     
  1014.                     while( sfb<12 )    {
  1015.                         sb = sfBandIndex[is_mpeg2][sfreq].s[sfb+1]-sfBandIndex[is_mpeg2][sfreq].s[sfb];
  1016.                         i = 3*sfBandIndex[is_mpeg2][sfreq].s[sfb] + j * sb;
  1017.                         for ( ; sb > 0; sb--) {
  1018.                             is_pos[i] = (*scalefac)[1].s[j][sfb];
  1019.                             i++;
  1020.                         }
  1021.                         sfb++;
  1022.                     }
  1023.                     sb = sfBandIndex[is_mpeg2][sfreq].s[11]-sfBandIndex[is_mpeg2][sfreq].s[10];
  1024.                     sfb = 3*sfBandIndex[is_mpeg2][sfreq].s[10] + j * sb;
  1025.                     sb = sfBandIndex[is_mpeg2][sfreq].s[12]-sfBandIndex[is_mpeg2][sfreq].s[11];
  1026.                     i = 3*sfBandIndex[is_mpeg2][sfreq].s[11] + j * sb;
  1027.                     
  1028.                     for ( ; sb > 0; sb-- ) {
  1029.                         is_pos[i] = is_pos[sfb];
  1030.                         i++;
  1031.                     }
  1032.                 }
  1033.                 if ( max_sfb <= 3 ) {
  1034.                     i = 2;
  1035.                     ss = 17;
  1036.                     sb = -1;
  1037.                     while ( i >= 0 ) {
  1038.                         if ( lr[1][i*SSLIMIT+ss] != 0.0 ) {
  1039.                             sb = i*18+ss;
  1040.                             i = -1;
  1041.                         } else {
  1042.                             ss--;
  1043.                             if ( ss < 0 ) {
  1044.                                 i--;
  1045.                                 ss = 17;
  1046.                             }
  1047.                         }
  1048.                     }
  1049.                     i = 0;
  1050.  
  1051.                     while ( sfBandIndex[is_mpeg2][sfreq].l[i] <= sb )
  1052.                         i++;
  1053.  
  1054.                     sfb = i;
  1055.                     i = sfBandIndex[is_mpeg2][sfreq].l[i];
  1056.  
  1057.                     for ( ; sfb<8; sfb++ ) {
  1058.                         sb = sfBandIndex[is_mpeg2][sfreq].l[sfb+1]-sfBandIndex[is_mpeg2][sfreq].l[sfb];
  1059.                         for ( ; sb > 0; sb--) {
  1060.                             is_pos[i] = (*scalefac)[1].l[sfb];
  1061.                             i++;
  1062.                         }
  1063.                     }
  1064.                 }
  1065.             } else {
  1066.                 for ( j=0; j<3; j++ ) {
  1067.                     int sfbcnt;
  1068.                     sfbcnt = -1;
  1069.                     
  1070.                     for( sfb=12; sfb >=0; sfb-- ) {
  1071.                         int lines;
  1072.                         lines = sfBandIndex[is_mpeg2][sfreq].s[sfb+1]-sfBandIndex[is_mpeg2][sfreq].s[sfb];
  1073.                         i = 3*sfBandIndex[is_mpeg2][sfreq].s[sfb] + (j+1) * lines - 1;
  1074.                         
  1075.                         while ( lines > 0 ) {
  1076.                             if ( lr[1][i] != 0.0 ) {
  1077.                                 sfbcnt = sfb;
  1078.                                 sfb = -10;
  1079.                                 lines = -10;
  1080.                             }
  1081.                             lines--;
  1082.                             i--;
  1083.                         }
  1084.                     }
  1085.                     sfb = sfbcnt + 1;
  1086.                     
  1087.                     while( sfb<12 ) {
  1088.                         sb = sfBandIndex[is_mpeg2][sfreq].s[sfb+1]-sfBandIndex[is_mpeg2][sfreq].s[sfb];
  1089.                         i = 3*sfBandIndex[is_mpeg2][sfreq].s[sfb] + j * sb;
  1090.                         for ( ; sb > 0; sb--) {
  1091.                             is_pos[i] = (*scalefac)[1].s[j][sfb];
  1092.                                                         
  1093.                             i++;
  1094.                         }
  1095.                         sfb++;
  1096.                     }
  1097.     
  1098.                     sb = sfBandIndex[is_mpeg2][sfreq].s[11]-sfBandIndex[is_mpeg2][sfreq].s[10];
  1099.                     sfb = 3*sfBandIndex[is_mpeg2][sfreq].s[10] + j * sb;
  1100.                     sb = sfBandIndex[is_mpeg2][sfreq].s[12]-sfBandIndex[is_mpeg2][sfreq].s[11];
  1101.                     i = 3*sfBandIndex[is_mpeg2][sfreq].s[11] + j * sb;
  1102.  
  1103.                     for ( ; sb > 0; sb-- ) {
  1104.                         is_pos[i] = is_pos[sfb];
  1105.                         i++;
  1106.                     }
  1107.                 }
  1108.             }
  1109.         } else {
  1110.             i = 31;
  1111.             ss = 17;
  1112.             sb = 0;
  1113.  
  1114.             while ( i >= 0 ) {
  1115.                 if ( lr[1][i*SSLIMIT+ss] != 0.0 ) {
  1116.                     sb = i*18+ss;
  1117.                     i = -1;
  1118.                 } else {
  1119.                     ss--;
  1120.                     if ( ss < 0 ) {
  1121.                         i--;
  1122.                         ss = 17;
  1123.                     }
  1124.                 }
  1125.             }
  1126.             i = 0;
  1127.  
  1128.             while ( sfBandIndex[is_mpeg2][sfreq].l[i] <= sb )
  1129.                 i++;
  1130.             sfb = i;
  1131.             i = sfBandIndex[is_mpeg2][sfreq].l[i];
  1132.  
  1133.             for ( ; sfb<21; sfb++ )    {
  1134.                 sb = sfBandIndex[is_mpeg2][sfreq].l[sfb+1] - sfBandIndex[is_mpeg2][sfreq].l[sfb];
  1135.                 for ( ; sb > 0; sb--) {
  1136.                     is_pos[i] = (*scalefac)[1].l[sfb];
  1137.                     i++;
  1138.                 }
  1139.             }
  1140.             sfb = sfBandIndex[is_mpeg2][sfreq].l[20];
  1141.             for ( sb = 576 - sfBandIndex[is_mpeg2][sfreq].l[21]; sb > 0; sb-- )
  1142.             {
  1143.                 is_pos[i] = is_pos[sfb];
  1144.                 i++;
  1145.             }
  1146.         }
  1147.  
  1148.         if (is_mpeg2) {    // BROKEN, esp with short blocks!!
  1149.             int ms_mode = !!(mode_ext & 2);
  1150.             int intensity_scale = gr_info->scalefac_compress&1;
  1151.             int is_max[54];
  1152.  
  1153.             // figure out illegal values for each scale factor band
  1154.  
  1155.             k=0;
  1156.             for(i=0; i<4; i++) {
  1157.                 for(j=0; j<scale_block_indexes[i]; j++)
  1158.                     is_max[k++] = (1<<slen[i])-1;
  1159.             }
  1160.             while(k<54) is_max[k++]=0;
  1161.  
  1162.             sfb=0;
  1163.             next_sfb_boundary = sfBandIndex[1][sfreq].l[1];
  1164.  
  1165.             for(i=0; i<SBLIMIT*SSLIMIT; i++) {
  1166.                 double x = lr[0][i];
  1167.  
  1168.                 lr[0][i] = x*is_ratio2[intensity_scale][ms_mode][is_max[sfb]+is_pos[i]][0];
  1169.                 lr[1][i] = x*is_ratio2[intensity_scale][ms_mode][is_max[sfb]+is_pos[i]][1];
  1170.  
  1171.                 if (i == next_sfb_boundary)
  1172.                     next_sfb_boundary = sfBandIndex[1][sfreq].l[++sfb + 1];
  1173.             }
  1174.         } else {
  1175.             for(i=0; i<SBLIMIT*SSLIMIT; i++) {
  1176.                 if ( is_pos[i] == 7 ) {
  1177.                     if ( ms_stereo ) {
  1178.                         double a = lr[0][i];
  1179.                         double b = lr[1][i];
  1180.  
  1181.                         lr[0][i] = (a+b)*0.707106781186547524400844362104849; ///1.41421356;
  1182.                         lr[1][i] = (a-b)*0.707106781186547524400844362104849; ///1.41421356;
  1183.                     }
  1184.                 } else {
  1185.                     double x = lr[0][i];
  1186.  
  1187.                     lr[0][i] = x*is_ratio1[is_pos[i]][0];
  1188.                     lr[1][i] = x*is_ratio1[is_pos[i]][1];
  1189.                 }
  1190.             }
  1191.         }
  1192.  
  1193.         return nz0>nz1 ? nz0 : nz1;
  1194.     } else if (ms_stereo) {
  1195.    
  1196.         if (nz1 > nz0)
  1197.             nz0 = nz1;
  1198.  
  1199.         for(i=0; i<nz0; i++) {
  1200.             double a = lr[0][i];
  1201.             double b = lr[1][i];
  1202.  
  1203.             lr[0][i] = (a+b)*0.707106781186547524400844362104849; ///1.41421356;
  1204.             lr[1][i] = (a-b)*0.707106781186547524400844362104849; ///1.41421356;
  1205.         }
  1206.  
  1207.         return nz0;
  1208.     }
  1209.  
  1210.     return -1;
  1211. }
  1212.  
  1213. void AMPDecoder::L3_Reorder (float xr[SBLIMIT*SSLIMIT],
  1214.             float ro[SBLIMIT*SSLIMIT],
  1215.             struct gr_info_s *gr_info) {
  1216.  
  1217.     int sfreq=sr_index;
  1218.     int sfb, sfb_start, sfb_lines;
  1219.     int sb, ss, window, freq, src_line, des_line;
  1220.     int i;
  1221.     
  1222.     if (gr_info->window_switching_flag && (gr_info->block_type == 2)) {
  1223.         if (gr_info->mixed_block_flag) {
  1224.             /* NO REORDER FOR LOW 2 SUBBANDS */
  1225.             for(i=0; i<2*SSLIMIT; i++)
  1226.                 ro[i] = xr[i];
  1227.  
  1228.             for(i=2*SSLIMIT; i<SBLIMIT*SSLIMIT; i++)
  1229.                 ro[i] = 0;
  1230.         
  1231.             /* REORDERING FOR REST SWITCHED SHORT */
  1232.  
  1233.             sfb_start=sfBandIndex[is_mpeg2][sfreq].s[3];
  1234.             sfb_lines=sfBandIndex[is_mpeg2][sfreq].s[4] - sfb_start;
  1235.             for(sfb=3; sfb < 13; sfb++) {
  1236.                 for(window=0; window<3; window++)
  1237.                     for(freq=0;freq<sfb_lines;freq++) {
  1238.                         src_line = sfb_start*3 + window*sfb_lines + freq; 
  1239.                         des_line = (sfb_start*3) + window + (freq*3);
  1240.                         ro[des_line] = xr[src_line];
  1241.                     }
  1242.  
  1243.                 sfb_start=sfBandIndex[is_mpeg2][sfreq].s[sfb];
  1244.                 sfb_lines=sfBandIndex[is_mpeg2][sfreq].s[sfb+1] - sfb_start;
  1245.             }
  1246.         } 
  1247.         else {  /* pure short */
  1248.             for(i=0; i<SBLIMIT*SSLIMIT; i++)
  1249.                 ro[i] = 0;
  1250.  
  1251.             sfb_start = 0;
  1252.             sfb_lines = sfBandIndex[is_mpeg2][sfreq].s[1];
  1253.  
  1254.             for(sfb=0; sfb < 13; sfb++) {
  1255.                 for(window=0; window<3; window++)
  1256.                     for(freq=0;freq<sfb_lines;freq++) {
  1257.                         src_line = sfb_start*3 + window*sfb_lines + freq; 
  1258.                         des_line = (sfb_start*3) + window + (freq*3);
  1259.                         ro[des_line] = xr[src_line];
  1260.                     }
  1261.  
  1262.                 sfb_start = sfBandIndex[is_mpeg2][sfreq].s[sfb];
  1263.                 sfb_lines = sfBandIndex[is_mpeg2][sfreq].s[sfb+1] - sfb_start;
  1264.             }
  1265.         }
  1266.  
  1267.         memcpy(xr, ro, sizeof(xr[0])*SBLIMIT*SSLIMIT);
  1268.     }
  1269. //    else {   /*long blocks */
  1270. //        memcpy(ro, xr, sizeof(xr[0])*SBLIMIT*SSLIMIT);
  1271. ////        for(i=0; i<SBLIMIT*SSLIMIT; i++)
  1272. ////                ro[i] = xr[i];
  1273. //    }
  1274. }
  1275.  
  1276. void AMPDecoder::L3_Antialias(float xr[SBLIMIT][SSLIMIT],
  1277.                     struct gr_info_s *gr_info) {
  1278.  
  1279.     static const double cs[8]={
  1280.         0.8574929166930334,
  1281.         0.8817419876991212,
  1282.         0.9496286453969656,
  1283.         0.9833145920724280,
  1284.         0.9955178161793186,
  1285.         0.9991605581318322,
  1286.         0.9998991952431355,
  1287.         0.9999931550702761,
  1288.     };
  1289.  
  1290.     static const double ca[8]={
  1291.         -0.5144957704600444,
  1292.         -0.4717319865436337,
  1293.         -0.3133774654334998,
  1294.         -0.1819132018778039,
  1295.         -0.09457419135028555,
  1296.         -0.04096558401494271,
  1297.         -0.01419856866483041,
  1298.         -0.003699974674877601,
  1299.     };
  1300.  
  1301.     double        bu,bd;  /* upper and lower butterfly inputs */
  1302.     int           ss,sb,sblim;
  1303.     
  1304.     /* clear all inputs */  
  1305.     
  1306. //    for(sb=0;sb<SBLIMIT;sb++)
  1307. //        for(ss=0;ss<SSLIMIT;ss++)
  1308. //            hybridIn[sb][ss] = xr[sb][ss];
  1309.  
  1310. /*    if  (gr_info->window_switching_flag && (gr_info->block_type == 2) &&
  1311.         !gr_info->mixed_block_flag ) {
  1312.  
  1313.         return;
  1314.     }*/
  1315.     
  1316.     if ( gr_info->window_switching_flag && gr_info->mixed_block_flag &&
  1317.         (gr_info->block_type == 2))
  1318.         sblim = 1;
  1319.     else
  1320.         sblim = SBLIMIT-1;
  1321.  
  1322.    /* 31 alias-reduction operations between each pair of sub-bands */
  1323.    /* with 8 butterflies between each pair                         */
  1324.  
  1325.    for(sb=0;sb<sblim;sb++)
  1326.        for(ss=0;ss<8;ss++) {
  1327.            bu = xr[sb][17-ss];
  1328.            bd = xr[sb+1][ss];
  1329.            xr[sb][17-ss] = (bu * cs[ss]) - (bd * ca[ss]);
  1330.            xr[sb+1][ss] = (bd * cs[ss]) + (bu * ca[ss]);
  1331.        }  
  1332. }
  1333.  
  1334. extern void inv_mdct(float *in,
  1335.                 float out[SSLIMIT][SBLIMIT],
  1336.                 float *prevblock,
  1337.                 int block_type);
  1338.  
  1339. void AMPDecoder::L3_Hybrid(float fsIn[SBLIMIT][SSLIMIT],
  1340.                 float fsOut[SSLIMIT][SBLIMIT],
  1341.                 int ch,
  1342.                 struct gr_info_s *gr_info,
  1343.                 int nonzero_entries) {
  1344.  
  1345.     int sb;
  1346.     float rawout[36];
  1347.     int bt;
  1348.     int bands;
  1349.  
  1350.     bands = (nonzero_entries + SSLIMIT-1) / SSLIMIT;
  1351.  
  1352.     for(sb=0; sb<bands; sb++) {
  1353.         bt = (gr_info->window_switching_flag && gr_info->mixed_block_flag &&
  1354.             (sb < 2)) ? 0 : gr_info->block_type; 
  1355.  
  1356.         inv_mdct(fsIn[sb], (float(*)[SBLIMIT])&fsOut[0][sb], prevblck[ch][sb], bt);
  1357.     }
  1358.  
  1359.     for(; sb<SBLIMIT; sb++) {
  1360.         for(int i=0; i<18; i++) {
  1361.             fsOut[i][sb] = prevblck[ch][sb][i];
  1362.             prevblck[ch][sb][i] = 0.0;
  1363.         }
  1364.     }
  1365. }
  1366.